package dodo.mindmap;

import processing.core.PApplet;
import processing.core.PConstants;
import geomerative.RFont;
import geomerative.RGroup;
import geomerative.RPath;
import geomerative.RPoint;
import geomerative.RShape;

public class NodeButton extends RGroup implements PConstants {
	
	RShape button, text;
	Node parent;
	NodeButtonAction action;
	
	public NodeButton(Node parent, RPoint center, float rotation, RShape button, RShape text, NodeButtonAction action){
		super();
		
		this.button = button;
		this.text = text;
		this.parent = parent;
		this.action = action;
		
		addElement(button);
		addElement(text);
		
		translate(center);
		rotate(rotation, center);
		
		if( PApplet.abs(rotation) > HALF_PI ){ //TODO: Rotate Text
			this.text.rotate(PI, this.text.getCenter());
		}
	}
	
	public static RShape circularButton(float innerRad, float outerRad, float angle){
		RShape button = RShape.createRing(0, 0, outerRad, innerRad);
		int npts = (int) (angle / HALF_PI);
		RPoint first = new RPoint(outerRad * 2, 0);
		first.rotate(- angle/2 - HALF_PI);
		
		RPath cutpath = new RPath(new RPoint(0,0));
		cutpath.addLineTo(first);
		for( int i=1; i <= npts; i++ ){
			RPoint intermediate = new RPoint(first);
			intermediate.rotate((i) * HALF_PI);
			cutpath.addLineTo(intermediate);
		}
		RPoint last = new RPoint(first);
		last.rotate(angle);
		cutpath.addLineTo(last);
		cutpath.addClose();
		
		RShape cut = new RShape(cutpath);
		
		return button.intersection(cut);
	}
	
	public static RShape text(String s, RFont font, float vCenter){
		RShape text = font.toShape(s);
		text.translate(0, -vCenter);
		return text;
	}
	
	public boolean clicked(RPoint p){
		return contains(p);
	}
}
